package com.qqgame.hlddz;

import android.app.ActivityOptions;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.qqgame.hlddz.databinding.ActivityMainBinding;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    public static int display = 2;

//    public static String baiduPackageName = "com.baidu.naviauto.baidu";
    public static String baiduPackageName = "com.ss.android.ugc.aweme";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        com.qqgame.hlddz.databinding.ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.open.setOnClickListener(view -> openBaidu());

        binding.openSettings.setOnClickListener(view -> settingsActivity());

        SharedPreferences sharedPreferences = getSharedPreferences("MyPreferences", MODE_PRIVATE);
        String display2AppValue = sharedPreferences.getString("display2App", "1");

        if ("1".equals(display2AppValue) || "".equals(display2AppValue)) {
            openGaoDe();
        }
        if ("2".equals(display2AppValue)) {
            openBaidu();
        }
        if ("3".equals(display2AppValue)) {
            openGaoDe();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            openBaidu();
        }

        finish();

    }

    private void openGaoDe() {
        try {
            ActivityOptions options = ActivityOptions.makeBasic();
            //这里一直display0是第一块屏；display1是第二块屏
            options.setLaunchDisplayId(display);
            Intent secondIntent = new Intent();
            ComponentName cn = new ComponentName("com.autonavi.amapautp", "com.autonavi.amapauto.MainMapActivity");
            secondIntent.setComponent(cn);
            //该句很重要，不添加则无法推送到副屏
            secondIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            //secondIntent.setData(Uri.parse("androidauto://navi?sourceApplication=Speech&dev=0&poiname=lip&lat=39.941711&lon=116.437630"));
            secondIntent.setData(Uri.parse("androidauto://rootmap"));
            // 使用ActivityOptions指定显示屏幕为Intent增加标志Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(secondIntent, options.toBundle());
        } catch (Exception e) {
            Toast.makeText(this, "e：" + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    private void openBaidu() {
        try {
            ActivityOptions options = ActivityOptions.makeBasic();
//            这里一直display0是第一块屏；display1是第二块屏
            options.setLaunchDisplayId(display);
            Intent secondIntent = new Intent();
            ComponentName cn = new ComponentName(baiduPackageName, "com.baidu.baidumaps.MapsActivity");
            secondIntent.setComponent(cn);
            //该句很重要，不添加则无法推送到副屏
            secondIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//            secondIntent.setData(Uri.parse("baidumap://map/navi?location=39.918199,116.411732&coord_type=bd09ll&query=酒店&src=webapp.companyName.appName"));
//            secondIntent.setData(Uri.parse("baidumap://map/navi"));
//            secondIntent.setData(Uri.parse("baidumap://map/navi/common?addr=company&coord_type=gcj02&src=webapp.companyName.appName"));
//            secondIntent.setData(Uri.parse("baidumap://map/navi/common?addr=home&coord_type=gcj02&src=webapp.companyName.appName"));
            startActivity(secondIntent, options.toBundle());
//            startActivity(secondIntent);
        } catch (Exception e) {
            Toast.makeText(this, "e：" + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    private void settingsActivity() {
        startActivity(new Intent(MainActivity.this, SettingsActivity.class));
    }

    private void GoHomeActivity() {
        startActivity(new Intent(MainActivity.this, GoHomeActivity.class));
    }

    private void GoCompanyActivity() {
        startActivity(new Intent(MainActivity.this, GoCompanyActivity.class));
    }


}